﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Threading;
using System.IO;
using System.Windows.Media.Imaging;

namespace Hugo
{
    public class SlideshowUIPage : UIPage
    {

        private BackgroundCanvas bgCanvas;
        private SlideShowTheme theme;
        private const int SECONDS_TO_CHANGE = 2;
        int currentIndex;

        public SlideshowUIPage(String name, BitmapSource u, SlideShowTheme theme) : base(name,u)
        {
            this.bgCanvas = new BackgroundCanvas();
            this.theme = theme;
            this.PageCanvas.Children.Add(bgCanvas);
        }

        DispatcherTimer timeTimer;

        public void stopSlideShow()
        {
            if (theme == null) { return; }
            if (timeTimer != null) { timeTimer.Stop(); }
        }

        public void startSlideshow()
        {
            if (theme == null) { return; }
            if (timeTimer != null) { timeTimer.Stop(); }
            currentIndex = 0;

            bgCanvas.clearBg();

            if (this.theme.bgs.Count > 1)
            {
                timeTimer = new DispatcherTimer();
                timeTimer.Interval = TimeSpan.FromSeconds(SECONDS_TO_CHANGE);
                timeTimer.Tick += new EventHandler(changeBg);
                timeTimer.Start();
            }
            changeBg(null, null);
        }

        public void changeBg(Object sender, EventArgs e)
        {
            currentIndex = (currentIndex + 1) % this.theme.bgs.Count;
            bgCanvas.setBackground(this.theme.bgs[currentIndex]);
        }

        public class SlideShowTheme
        {
            public List<System.Drawing.Bitmap> bgs;

            public SlideShowTheme(List<System.Drawing.Bitmap> bgs)
            {
                this.bgs = bgs;
            }
        }

        public static SlideShowTheme getIconTheme()
        {
            List<System.Drawing.Bitmap> bgs = new List<System.Drawing.Bitmap>();
            
            DirectoryInfo dir = new DirectoryInfo(GlobalSettings.PhotoPath);
            foreach (FileInfo f in dir.GetFiles())
            {
                
                string name = f.FullName;

                if (name.ToLower().EndsWith("jpg"))
                {
                    bgs.Add(new System.Drawing.Bitmap(name));
                }
            }

            return new SlideShowTheme(bgs);

        }

        public static SlideShowTheme getNatureTheme()
        {
            List<System.Drawing.Bitmap> bgs = new List<System.Drawing.Bitmap>();

            bgs.Add(Properties.Resources.nature_1);
            bgs.Add(Properties.Resources.nature_10);
            bgs.Add(Properties.Resources.nature_11);
            bgs.Add(Properties.Resources.nature_13);
            bgs.Add(Properties.Resources.nature_14);
            bgs.Add(Properties.Resources.nature_15);
            bgs.Add(Properties.Resources.nature_16);
            bgs.Add(Properties.Resources.nature_17);
            bgs.Add(Properties.Resources.nature_18);
            bgs.Add(Properties.Resources.nature_19);
            bgs.Add(Properties.Resources.nature_2);
            bgs.Add(Properties.Resources.nature_20);
            bgs.Add(Properties.Resources.nature_21);
            bgs.Add(Properties.Resources.nature_22);
            bgs.Add(Properties.Resources.nature_23);
            bgs.Add(Properties.Resources.nature_24);
            bgs.Add(Properties.Resources.nature_25);
            bgs.Add(Properties.Resources.nature_26);
            bgs.Add(Properties.Resources.nature_27);
            bgs.Add(Properties.Resources.nature_28);
            bgs.Add(Properties.Resources.nature_3);
            bgs.Add(Properties.Resources.nature_4);
            bgs.Add(Properties.Resources.nature_5);
            bgs.Add(Properties.Resources.nature_6);
            bgs.Add(Properties.Resources.nature_7);
            bgs.Add(Properties.Resources.nature_8);
            bgs.Add(Properties.Resources.nature_9);

            return new SlideShowTheme(bgs);

        }

        public static SlideShowTheme getSimpleTheme()
        {
            List<System.Drawing.Bitmap> bgs = new List<System.Drawing.Bitmap>();

            bgs.Add(Properties.Resources.blackspotlight);

            return new SlideShowTheme(bgs);

        }

        public override void setPage()
        {
            bgCanvas.Opacity = 1;
            startSlideshow();
            base.setPage();
        }

        public override void unSetPage()
        {
            bgCanvas.Opacity = 0;
            stopSlideShow();
            base.setPage();
        }

    }
}
